package com.zlzxm.shoesmarket.viewmodel;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.zlzxm.shoesmarket.TestConstant;
import com.zlzxm.shoesmarket.util.RxAndroidHelp;
import com.zlzxm.shoesmarket.view.adapter.entity.EntityHomeThing;
import com.zlzxm.shoesmarket.viewmodel.entity.EntityHomeFilter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * CREATE BY zlzxm ON 4/8/21
 */
public class VMHomeSort extends ViewModel {

    private static final String TAG = "VMHomeSort";


    public static final int SORT_DAY = 1;
    public static final int SORT_WEEK = 2;
    public static final int SORT_MONTH = 3;


    private boolean isFirstLoad = true;

    private  MutableLiveData<List<EntityHomeThing>> liveData = new MutableLiveData<>();

    private MutableLiveData<List<EntityHomeFilter>> liveFilter = new MutableLiveData<>();

    public MutableLiveData<List<EntityHomeThing>> getLiveData() {
        return liveData;
    }

    public MutableLiveData<List<EntityHomeFilter>> getLiveFilter() {
        return liveFilter;
    }

    public boolean isFirstLoad() {
        return isFirstLoad;
    }

    public void getData(int sort){

        Log.e(TAG,"getData");

        Observable.timer(3, TimeUnit.SECONDS)
                .compose(RxAndroidHelp.turn())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Long aLong) {

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                        Log.e(TAG,"onComplete:");
                        isFirstLoad = false;
                        liveData.setValue(createTestData(sort));

                    }
                });

    }


    public void requestFilter(){


        Observable.timer(3,TimeUnit.SECONDS)
                .compose(RxAndroidHelp.turn())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Long aLong) {

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                        liveFilter.setValue(createTestFilter());
                    }
                });



    }

    //<editor-fold defaultState="collapsed" desc="test">


    private List<EntityHomeThing> createTestData(int sort){

        String first;

        String second;
        if(sort  % 2 == 0) {

            first = TestConstant.PIC_ONE;

            second = TestConstant.PIC_TWO;

        }else {

            first = TestConstant.PIC_TWO;

            second = TestConstant.PIC_ONE;
        }

        List<EntityHomeThing> result = new ArrayList<>();

        for (int i = 0; i < 50; i++) {

            String pic = i % 2 == 0? first:second;

            result.add(EntityHomeThing.creator()
                    .title(sort+"耐克闪电闪电是"+i)
                    .desc("#"+i+i+i+i+i+i+i+0)
                    .hotNum(" "+i+1)
                    .url(pic));
        }

        return result;
    }


    private List<EntityHomeFilter> createTestFilter(){

        List<EntityHomeFilter> list = new ArrayList<>();

        EntityHomeFilter entityHomeFilter1 = new EntityHomeFilter();
        entityHomeFilter1.setTitle("李宁");

        EntityHomeFilter entityHomeFilter2 = new EntityHomeFilter();
        entityHomeFilter2.setTitle("耐克");


        EntityHomeFilter entityHomeFilter3 = new EntityHomeFilter();
        entityHomeFilter3.setTitle("阿迪");

        EntityHomeFilter entityHomeFilter4 = new EntityHomeFilter();
        entityHomeFilter4.setTitle("安踏");


        list.add(entityHomeFilter1);
        list.add(entityHomeFilter2);
        list.add(entityHomeFilter3);
        list.add(entityHomeFilter4);

        return list;
    }

    // </editor-fold>


}
